package com.kaluli.project.service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kaluli.project.domain.entity.KllMainOrder;
import com.kaluli.project.domain.entity.KllMq;
import com.kaluli.project.domain.entity.KllOrderLogistics;
import com.kaluli.project.domain.entity.KllOrderSynApi;
import com.kaluli.project.utils.RandomNumber;

public class TradeThread implements Runnable {

	private volatile boolean stopFlag = true;

	/**
	 * 设置停止标志
	 * 
	 * @param stopFlag
	 */
	public void stopTrade() {
		this.stopFlag = true;
		// Thread.currentThread().interrupt();
	}

	// 判断停止请求是否已经送出
	public final boolean isStopTrade() {
		return this.stopFlag;
	}

	private String msgChannel; // 第三方渠道

	public String getMsgChannel() {
		return msgChannel;
	}

	public void setMsgChannel(String msgChannel) {
		this.msgChannel = msgChannel;
	}

	private TradeContext context;

	private TradeQueue tradeQueue = null;

	public TradeThread(String msgChannel, TradeContext context, TradeQueue tradeQueue) {
		this.msgChannel = msgChannel;
		this.context = context;
		this.tradeQueue = tradeQueue;
	}

	@Override
	public void run() {
		try {
			stopFlag = false;
			while (!stopFlag) {
				KllMainOrderService service = context.getKllMainOrderService();
				KllMqService mqService = context.getKllMqService();
				if (!context.getMsgChannel().equals("ZZ-out")) {
					KllOrderSynApi order = tradeQueue.getQueue();
					if(context.getMsgChannel().equals("HG")){
						SimpleDateFormat sFormat = new SimpleDateFormat("YYYYMMddHHmmss");
//						String msg = mqService.getMsgId(order.getOrderNumber(),this.msgChannel,"0000");
						synchronized (this) {
							String msg = RandomNumber.getRandom();
							String endfix = sFormat.format(new Date())+msg;
							String[] response = service.sendXml(order.getOrderNumber(), this.msgChannel,endfix);
						}
						//service.createSendLog(order.getOrderNumber(), response[1], response[0], this.msgChannel,"Trade",response[2]);
					}else if(context.getMsgChannel().equals("GJ")){
						SimpleDateFormat sFormat = new SimpleDateFormat("YYYYMMddHHmmss");
//						String msg = mqService.getMsgId(order.getOrderNumber(),this.msgChannel,"000");
						synchronized (this) {
							String msg = RandomNumber.getRandom();
							String endfix = sFormat.format(new Date())+msg;
							String[] response = service.sendXml(order.getOrderNumber(), this.msgChannel,endfix);
						}
						//service.createSendLog(order.getOrderNumber(), response[1], response[0], this.msgChannel,"Trade",response[2]);
					}else if(context.getMsgChannel().equals("ZZ")){
						SimpleDateFormat sFormat = new SimpleDateFormat("YYYYMMddHHmmss");
						String msg = mqService.getMsgId(order.getOrderNumber(),this.msgChannel,"000");
						String endfix = sFormat.format(new Date())+msg;
						String[] response = service.sendXml(order.getOrderNumber(), this.msgChannel,endfix);
						//service.createSendLog(order.getOrderNumber(), response[1], response[0], this.msgChannel,"Trade",endfix);
					}else if(context.getMsgChannel().equals("AlipayHg")){
						SimpleDateFormat sFormat = new SimpleDateFormat("YYYYMMddHHmmss");
						String msg = mqService.getMsgId(order.getOrderNumber(),this.msgChannel,"000");
						String endfix = sFormat.format(new Date())+msg;
						String[] response = service.sendXml(order.getOrderNumber(), this.msgChannel,endfix);
					}else if(context.getMsgChannel().equals("AlipayGj")){
						SimpleDateFormat sFormat = new SimpleDateFormat("YYYYMMddHHmmss");
						String msg = mqService.getMsgId(order.getOrderNumber(),this.msgChannel,"000");
						String endfix = sFormat.format(new Date())+msg;
						String[] response = service.sendXml(order.getOrderNumber(), this.msgChannel,endfix);
					}else if(context.getMsgChannel().equals("Weixin")){
						SimpleDateFormat sFormat = new SimpleDateFormat("YYYYMMddHHmmss");
						String msg = mqService.getMsgId(order.getOrderNumber(),this.msgChannel,"000");
						String endfix = sFormat.format(new Date())+msg;
						String[] response = service.sendXml(order.getOrderNumber(), this.msgChannel,endfix);
						
					}else if(context.getMsgChannel().equals("EDI")){
						SimpleDateFormat sFormat = new SimpleDateFormat("YYYYMMddHHmmss");
						String msg = mqService.getMsgId(order.getOrderNumber(),this.msgChannel,"000");
						String endfix = sFormat.format(new Date())+msg;
						String[] response = service.sendXml(order.getOrderNumber(), this.msgChannel,endfix);
						
					}
					
					
				} else { //处理卓志回调的物流信息
					KllOrderSynApi order = tradeQueue.getQueue();
					if(order.getLogisticJSON()!=null){
						JSONObject ja = JSON.parseObject(order.getLogisticJSON()); //简单物流信息
						//KllOrderLogistics ol = new KllOrderLogistics();
						String express_no = ja.getString("wayBillNo");
						String express_name = ja.getString("logisticsname");
						String express_note = ja.getString("notes");
						KllMainOrderDetailService kllMainOrderDetailService = context.getKllMainOrderDetailService();
						KllMainOrderService kllMainOrderService = context.getKllMainOrderService();
						KllMainOrder mainOrder = service.findKllMainOrderByIbillingNumber(order.getIbillingNumber());
						//更新EDB库存
//						kllMainOrderService.updateEDBstock(mainOrder.getIbillingNumber());
						int status = kllMainOrderDetailService.callBackLogistics(mainOrder.getOrderNumber(), express_no, express_name, express_note, order.getLogisticJSON());
						kllMainOrderDetailService.updateEDILogistics(mainOrder.getOrderNumber());
						String msgId = mqService.getMsgId(mainOrder.getOrderNumber(),"ZZ-out","000");
						if(status==0){
							service.createSendLog(mainOrder.getOrderNumber(), "{'status':'0'}", order.getLogisticJSON(), "ZZ-out","ZZ-out",msgId);
						}else{
							service.createSendLog(mainOrder.getOrderNumber(), "{'status':'1'}", order.getLogisticJSON(), "ZZ-out","ZZ-out",msgId);
						}
					}
					
				}
			}
		} catch (Exception e) {
			System.out.println(e);
		}

	}

}
